Modification start date
[BattleCats.git] / Assets / EZ Camera Shake / Scripts / CameraShaker.cs
blob7bfce00b8994c410c094aeedd146ecce74aac082
1 using UnityEngine;
2 using System.Collections.Generic;
4 namespace EZCameraShake
6 [AddComponentMenu("EZ Camera Shake/Camera Shaker")]
7 public class CameraShaker : MonoBehaviour
9 /// <summary>
10 /// The single instance of the CameraShake in the current scene. Do not use if you have multiple instances.
11 /// </summary>
12 public static CameraShaker Instance;
13 static Dictionary<string, CameraShaker> instanceList = new Dictionary<string, CameraShaker>();
15 /// <summary>
16 /// The default position influcence of all shakes created by this shaker.
17 /// </summary>
18 public Vector3 DefaultPosInfluence = new Vector3(0.15f, 0.15f, 0.15f);
19 /// <summary>
20 /// The default rotation influcence of all shakes created by this shaker.
21 /// </summary>
22 public Vector3 DefaultRotInfluence = new Vector3(1, 1, 1);
24 public static Vector3 camera_at_hit_time;
25 public static bool isHit;
27 Vector3 posAddShake, rotAddShake;
29 List<CameraShakeInstance> cameraShakeInstances = new List<CameraShakeInstance>();
31 void Awake()
33 Instance = this;
34 instanceList.Add(gameObject.name, this);
37 void Update()
39 if (isHit) {
40 posAddShake = GameObject.FindGameObjectWithTag("CameraDestination").transform.localPosition;
41 rotAddShake = Vector3.zero;
44 for (int i = 0; i < cameraShakeInstances.Count; i++)
46 if (i >= cameraShakeInstances.Count)
47 break;
49 CameraShakeInstance c = cameraShakeInstances[i];
51 if (c.CurrentState == CameraShakeState.Inactive && c.DeleteOnInactive)
53 cameraShakeInstances.RemoveAt(i);
54 i--;
56 else if (c.CurrentState != CameraShakeState.Inactive)
58 posAddShake += CameraUtilities.MultiplyVectors(c.UpdateShake(), c.PositionInfluence);
59 rotAddShake += CameraUtilities.MultiplyVectors(c.UpdateShake(), c.RotationInfluence);
63 if (isHit) {
64 transform.localPosition = posAddShake;
65 transform.localEulerAngles = rotAddShake;
67 if (cameraShakeInstances.Count == 0)
68 isHit = false;
71 /// <summary>
72 /// Gets the CameraShaker with the given name, if it exists.
73 /// </summary>
74 /// <param name="name">The name of the camera shaker instance.</param>
75 /// <returns></returns>
76 public static CameraShaker GetInstance(string name)
78 CameraShaker c;
80 if (instanceList.TryGetValue(name, out c))
81 return c;
83 Debug.LogError("CameraShake " + name + " not found!");
85 return null;
88 /// <summary>
89 /// Starts a shake using the given preset.
90 /// </summary>
91 /// <param name="shake">The preset to use.</param>
92 /// <returns>A CameraShakeInstance that can be used to alter the shake's properties.</returns>
93 public CameraShakeInstance Shake(CameraShakeInstance shake)
95 cameraShakeInstances.Add(shake);
96 return shake;
99 /// <summary>
100 /// Shake the camera once, fading in and out over a specified durations.
101 /// </summary>
102 /// <param name="magnitude">The intensity of the shake.</param>
103 /// <param name="roughness">Roughness of the shake. Lower values are smoother, higher values are more jarring.</param>
104 /// <param name="fadeInTime">How long to fade in the shake, in seconds.</param>
105 /// <param name="fadeOutTime">How long to fade out the shake, in seconds.</param>
106 /// <returns>A CameraShakeInstance that can be used to alter the shake's properties.</returns>
107 public CameraShakeInstance ShakeOnce(float magnitude, float roughness, float fadeInTime, float fadeOutTime)
109 CameraShakeInstance shake = new CameraShakeInstance(magnitude, roughness, fadeInTime, fadeOutTime);
113 shake.PositionInfluence = DefaultPosInfluence;
114 shake.RotationInfluence = DefaultRotInfluence;
115 cameraShakeInstances.Add(shake);
117 return shake;
120 /// <summary>
121 /// Shake the camera once, fading in and out over a specified durations.
122 /// </summary>
123 /// <param name="magnitude">The intensity of the shake.</param>
124 /// <param name="roughness">Roughness of the shake. Lower values are smoother, higher values are more jarring.</param>
125 /// <param name="fadeInTime">How long to fade in the shake, in seconds.</param>
126 /// <param name="fadeOutTime">How long to fade out the shake, in seconds.</param>
127 /// <param name="posInfluence">How much this shake influences position.</param>
128 /// <param name="rotInfluence">How much this shake influences rotation.</param>
129 /// <returns>A CameraShakeInstance that can be used to alter the shake's properties.</returns>
130 public CameraShakeInstance ShakeOnce(float magnitude, float roughness, float fadeInTime, float fadeOutTime, Vector3 posInfluence, Vector3 rotInfluence)
132 CameraShakeInstance shake = new CameraShakeInstance(magnitude, roughness, fadeInTime, fadeOutTime);
133 shake.PositionInfluence = posInfluence;
134 shake.RotationInfluence = rotInfluence;
135 cameraShakeInstances.Add(shake);
137 isHit = true;
138 camera_at_hit_time = GameObject.FindGameObjectWithTag ("MainCamera").transform.position;
140 return shake;
143 /// <summary>
144 /// Start shaking the camera.
145 /// </summary>
146 /// <param name="magnitude">The intensity of the shake.</param>
147 /// <param name="roughness">Roughness of the shake. Lower values are smoother, higher values are more jarring.</param>
148 /// <param name="fadeInTime">How long to fade in the shake, in seconds.</param>
149 /// <returns>A CameraShakeInstance that can be used to alter the shake's properties.</returns>
150 public CameraShakeInstance StartShake(float magnitude, float roughness, float fadeInTime)
152 CameraShakeInstance shake = new CameraShakeInstance(magnitude, roughness);
153 shake.PositionInfluence = DefaultPosInfluence;
154 shake.RotationInfluence = DefaultRotInfluence;
155 shake.StartFadeIn(fadeInTime);
156 cameraShakeInstances.Add(shake);
157 return shake;
160 /// <summary>
161 /// Start shaking the camera.
162 /// </summary>
163 /// <param name="magnitude">The intensity of the shake.</param>
164 /// <param name="roughness">Roughness of the shake. Lower values are smoother, higher values are more jarring.</param>
165 /// <param name="fadeInTime">How long to fade in the shake, in seconds.</param>
166 /// <param name="posInfluence">How much this shake influences position.</param>
167 /// <param name="rotInfluence">How much this shake influences rotation.</param>
168 /// <returns>A CameraShakeInstance that can be used to alter the shake's properties.</returns>
169 public CameraShakeInstance StartShake(float magnitude, float roughness, float fadeInTime, Vector3 posInfluence, Vector3 rotInfluence)
171 CameraShakeInstance shake = new CameraShakeInstance(magnitude, roughness);
172 shake.PositionInfluence = posInfluence;
173 shake.RotationInfluence = rotInfluence;
174 shake.StartFadeIn(fadeInTime);
175 cameraShakeInstances.Add(shake);
176 return shake;
179 /// <summary>
180 /// Gets a copy of the list of current camera shake instances.
181 /// </summary>
182 public List<CameraShakeInstance> ShakeInstances
183 { get { return new List<CameraShakeInstance>(cameraShakeInstances); } }
185 void OnDestroy()
187 instanceList.Remove(gameObject.name);